/**
 * Created by a12301 on 2017/6/26.
 */

// 栈

class Stack {
  constructor() {
    this.items = []
  }

  // 入栈
  push(element) {
    this.items.push(element)
  }

  // 出栈
  pop() {
    return this.items.pop()
  }

  // 末位
  get peek() {
    return this.items[this.items.length - 1]
  }

  // 是否为空栈
  get isEmpty() {
    return !this.items.length
  }

  // 尺寸
  get size() {
    return this.items.length
  }

  // 清空栈
  clear() {
    this.items = []
  }

  // 打印栈数据
  print() {
    console.log(this.items.toString())
  }

}

// 实例化一个栈
const stack = new Stack()
console.log(stack.isEmpty) // true

// 添加元素
stack.push(5)
stack.push(8)

// 读取属性再添加
console.log(stack.peek) // 8
stack.push(11)
console.log(stack.size) // 3
console.log(stack.isEmpty) // false






// 列队

class Queue {

  constructor(items) {
    this.items = items || []
  }

  enqueue(element){
    this.items.push(element)
  }

  dequeue(){
    return this.items.shift()
  }

  front(){
    return this.items[0]
  }

  clear(){
    this.items = []
  }

  get size(){
    return this.items.length
  }

  get isEmpty(){
    return !this.items.length
  }

  print() {
    console.log(this.items.toString())
  }
}



const queue = new Queue()
console.log(queue.isEmpty) // true

queue.enqueue('John')
queue.enqueue('Jack')
queue.enqueue('Camila')
console.log(queue.size) // 3
console.log(queue.isEmpty) // false
queue.dequeue()
queue.dequeue()
queue.print() // 'Camila'



// https://juejin.im/post/594dfe795188250d725a220a?utm_source=gold_browser_extension